#include "ItemFactory.h"

Item_Consumable* Factory::makeConsumable(int item)
{
	Item_Consumable* newItem = (Item_Consumable*)(create(ITEM_CONSUMABLE));
	newItem->_copyInit(((Item_Consumable*)(m_originalCopy[item])));
	return newItem;
}
Item_Equippable* Factory::makeMaterial(int item)
{
	Item_Equippable* newItem = (Item_Equippable*)(create(ITEM_EQUIPPABLE));
	newItem->_copyInit(((Item_Equippable*)(m_originalCopy[item])));
	return newItem;
}
Item_Equippable* Factory::makeEquippable(int item)
{
	Item_Equippable* newItem = (Item_Equippable*)(create(ITEM_EQUIPPABLE));
	newItem->_copyInit(((Item_Equippable*)(m_originalCopy[item])));
	return newItem;
}
Item_Base*		 Factory::makeItem(ASSET item, int Type)
{
	Item_Base* newItem = (create(Type));
	switch(Type)
	{
	case ITEM_CONSUMABLE:	newItem = makeConsumable(item);	break;
	case ITEM_EQUIPPABLE:	newItem = makeEquippable(item);	break;
	case ITEM_MATERIAL:		newItem = makeMaterial(item);	break;
	default:										break;
	}
	return newItem;
}

//=====PROJECTILES
Item_Projectile* Factory::buildProjectile(Vector3D pos, float lastXDir, float magnitude, bool Physics, bool Movement, short Range,
	short Alignment, Vector3D PIE, ASSET item = alpha){
		Item_Projectile* newItem = (Item_Projectile*)(create(ITEM_PROJECTILE));
		newItem->SetPos(pos);
		newItem->active = true;
		newItem->assetIndex	= m_artAssets[item];
		newItem->objectType	= TYPE_ENTITY_PIECE;
		newItem->itemType	= ITEM_PROJECTILE;
		newItem->_projectileInit(Movement,Physics,PIE,Vector2D(magnitude,lastXDir),Range,Alignment);
		return newItem;
}
void Factory::makeCollider(Vector3D pos, float lastXDir, float magnitude, bool Physics, bool Movement, short Range,
	short Alignment, Vector3D PIE, short colliderType = Item_Projectile::SPELL_WEAPONRY, char* name = "Leaf")
{
	Item_Projectile* newItem = (Item_Projectile*)(create(ITEM_PROJECTILE));
	newItem->SetPos(pos);
	newItem->active = true;
	newItem->assetIndex	= m_artAssets[collider];
	newItem->objectType	= TYPE_ENTITY_PIECE;
	newItem->itemType	= ITEM_PROJECTILE;
	newItem->collideEffect = colliderType;
	newItem->_projectileInit(Movement,Physics,PIE,Vector2D(magnitude,lastXDir),Range,Alignment);
	newItem->itemName = name;
	addToDraw((BaseObject*)(newItem),DRAW_ENTITIES);
}
//=====CRAFTING
int Factory::getAssetEnum(Item_Base* toCheck)
{	
	if(toCheck)
	for(int i = 0; i < ITEM_SIZE; i++){
		if(toCheck->iconAsset.x == m_iconAssets[i].x
			&& toCheck->iconAsset.y == m_iconAssets[i].y)
			return i;
	}
	return -1;
}
void Factory::craftObject(vItemBase &Inventory, int ItemID)
{
	for(int x = 0; x < 4; x++)
	{
		for(int i = 0; i < Inventory.size(); i++)
		{
			if(recipeBook[ItemID].checkIfUsesIngredientAt(Inventory[i]->itemName,x))
			{
				Inventory.remove(i);
				break;
			}
		}
	}

	GUI.get().setMessage(m_originalCopy[ItemID]->itemName," crafted",GUI_Handler::NOTIFICATION);
	Inventory.push(makeItem(ASSET(ItemID),m_originalCopy[ItemID]->itemType));

}
vItemBase Factory::createRecipeBook(vItemBase Inventory)
{
	/*
	@param: recipebook- this is a set of strings tied to the objects and loaded
	at gamestart. Each recipe holds a set of booleans, if all 4 booleans are true
	then we have the ingredients to craft the item.
	*/

	vItemBase book;	//A vector of available recipes that the player can craft

	for(int i = 1; i < ITEM_SIZE; i++)
	{
		//First, clear all records in or recipe book.
		recipeBook[i].clear();	
		//Then poll our inventory and show it to the recipe book to build a new set of possible craftables.
		for(int x = 0; x < Inventory.size(); x++)				
			recipeBook[i].checkIfUses(Inventory[x]->itemName);	
		//Then go through and add a copy of each craftable item to the book to be handed back
		if(recipeBook[i].canBeCrafted())								
			book.push(makeItem(ASSET(i),m_originalCopy[i]->itemType));	
	}
		return book;
}